module(..., package.seeall) -- 使得文件中的函数在何处都可调用
require "pins" -- 用到了pin库，该库为luatask专用库，需要进行引用
require "sys"
require "getLbsLoc"
require "rtos"
require "uartTask"
require "utils"
require "mqtt"
require "misc"
require "audio"
require "math"
require "nvm"
require "send433RFSignal"
require "logGenerator"
require "mqttOutMsg"
require "httpTask"
--初始化设备参数文件
nvm.init("config.lua", true)
--参数初始化 magnitude:震级
local warningColor, toneDowns, toneBreak, doToneTask, lampDowns, lampBreak,
doLampTask, signalDowns, signalBreak, doSignalTask, sysTime, eqlog, errlog,
alarm433Bool, ttsStr, slen, eventID, producerID, eventCancelID, producerCancelID,
focal_longitude, focal_latitude, quake_intensity, quakeTimeStr, sendTime, magnitude, placeName,
msgCode, actionMark, alertStr, isEarthquakeReport, areaCode, depth =
0, 0, false, true, 0, false, true, 0, false, true, "", "", "", false, "",
    string.len, "", "", "", "", 0.0, 0.0, 0.0, "", "", 0.0, "", "",
    false, "", false, "510781006001", ""
TTS = 1
-- 地震预警系统
-- 两个功能：1.预警系统演练（tts语音播报3遍同时报警音触发）
-- 入参：jsondata接收的预警信息
local setGpio9Fnc = pins.setup(pio.P0_9, 1) -- 静音设置（高电平静音）
local setGpio13Fnc = pins.setup(pio.P0_13, 0) -- 预警指示
local setGpio11Fnc = pins.setup(pio.P0_11, 0) -- Buzzer正常低电平，预警高电平
local setGpio17Fnc = pins.setup(pio.P0_17, 0) -- 地震预警测试音量控制，高电平为测试音量（音量小），低电平为正常音量（音量大）
--local setGpio12Fnc = pins.setup(pio.P0_12, 0) -- 报警灯
-- 获取配置参数
local clientNo, longitude, latitude, configIntensity, enable433, countDown,
alertSound, alertLight, magnitudeConfig, centerIntensityConfig, emergencyLamp,
relay, intensityReturn, regCenter =
nvm.get("info")[1], nvm.get("longitude"), nvm.get("latitude"), tonumber(nvm.get("intensity")),
    nvm.get("enable433"), tonumber(nvm.get("countDown")), tonumber(nvm.get("alertSound")),
    tonumber(nvm.get("alertLight")), tonumber(nvm.get("magnitude")),
    tonumber(nvm.get("centerIntensity")), tonumber(nvm.get("emergencyLamp")),
    tonumber(nvm.get("relay")), tonumber(nvm.get("intensityReturn")), nvm.get("regCenter")
--地震预警上报
local function eathquakeWarningReportHttp(distance, typeWarning, second, intensity)
    local url,sendMsg, body = "http://218.89.239.88:9889/externalService/receive/emergencyInformation", "",{}
    sendMsg = "{\n" .. "\"eventId\":" .. "\"" .. eventID .. "\"," ..
        "\"quakeIntensity\":" .. "\"" .. quake_intensity .. "\"," ..
        "\"distance\":" .. "\"" .. distance .. "\"," ..
        "\"latitude\":" .. "\"" .. latitude .. "\"," ..
        "\"type\":" .. "\"" .. typeWarning .. "\"," ..
        "\"quakeTime\":" .. "\"" .. quakeTimeStr .. "\"," ..
        "\"sendTime\":" .. "\"" .. sendTime .. "\"," ..
        "\"second\":" .. "\"" .. second .. "\"," ..
        "\"intensity\":" .. "\"" .. intensity .. "\"," ..
        "\"areaCode\":" .. "\"" .. areaCode .. "\"," ..
        "\"depth\":" .. "\"" .. depth .. "\"," ..
        "\"location\":" .. "\"" .. placeName .. "\"," ..
        "\"magnitude\":" .. "\"" .. magnitude .. "\"," ..
        "\"longitude\":" .. "\"" .. longitude .. "\"}"
    body = { ["dataJson"] = sendMsg }
        log.info("sendMsg",sendMsg)
    httpTask.earthquakeWarningReport(url, nil, body, 6000, "earthquakeWarningReport")
end

--预警事件ID组装
local function warningEventIdAssemble(quakeTimeStr)
    local datePre, timeSur, y, m, d, h, M, s = "", "", "", "", "", "", "", ""
    datePre = string.split(quakeTimeStr, " ")[1]
    timeSur = string.split(quakeTimeStr," ")[2]
    y = string.split(datePre, "-")[1]
    m = string.split(datePre, "-")[2]
    d = string.split(datePre, "-")[3]
    h = string.split(timeSur, ":")[1]
    M = string.split(timeSur, ":")[2]
    s = string.split(timeSur, ":")[3]
    eventID = y..m..d..h..M..s..".0001_1"
    return eventID
end

--获取系统时间
local function getSystemTime()
    local tClock, nowDate = os.date("*t"), ""
    nowDate = string.format("%04d-%02d-%02d %02d:%02d:%02d", tClock.year, tClock.month, tClock.day, tClock.hour,
        tClock.min, tClock.sec)
    return nowDate
end

--发送433信号
local function send433SignalTask(countSignalDownS, intensity2F433)
    doSignalTask = false
    setGpio9Fnc(0)
    signalDowns = countSignalDownS
    send433RFSignal.InterruptSendMsg = true
    send433RFSignal.sendMsgTask()
    if not send433RFSignal.TimeId then
        send433RFSignal.signalRF433SerialCommunication(signalDowns, intensity2F433, "0", alarm433Bool)
    end
    sys.wait(1000)
    signalDowns = signalDowns - 1
    while signalDowns > 0 do
        if signalDowns == 1 then
            alarm433Bool = true
        end
        if not send433RFSignal.TimeId then
            send433RFSignal.signalRF433SerialCommunication(signalDowns, intensity2F433, "0", alarm433Bool)
        end
        if signalBreak then
            signalBreak = false
            signalDowns = 0
            break
        end
        sys.wait(1000)
        if signalBreak then
            signalBreak = false
            signalDowns = 0
            break
        end
        signalDowns = signalDowns - 1
    end
    setGpio9Fnc(1) -- 报警结束设置静音
    doSignalTask = true
    alarm433Bool = false
end

-- 预警声音播报任务
local function alarmVoiceTask(countDownS)
    setGpio9Fnc(0)
    --local count = 0
    doToneTask = false
    toneDowns = countDownS
    audio.play(TTS, "TTS", alertStr, 7)
    while toneDowns > 0 do
        log.info("toneDowns", toneDowns)
        if toneDowns <= 10 then
            ttsStr = tostring(toneDowns)
            audio.play(TTS, "TTS", ttsStr, 7)
        end
        if toneBreak then
            toneBreak = false
            toneDowns = 0
            --uartTask.write(0x0C)
            break
        end
        --if math.fmod(count, 12) == 0 then uartTask.write(0x0C) end
        sys.wait(1000)
        if toneBreak then
            toneBreak = false
            toneDowns = 0
            --uartTask.write(0x0C)
            break
        end
        toneDowns = toneDowns - 1
        --count = count + 1
    end
    setGpio9Fnc(1)
    doToneTask = true
end

--演习或演练声音播报
local function alarmDrillVoiceTask(countDownS)
    doToneTask = false
    setGpio9Fnc(0)
    setGpio13Fnc(1)
    toneDowns = countDownS
    audio.play(TTS, "TTS", ttsStr, 7)
    while toneDowns > 0 do
        if toneDowns <= 10 then
            log.info("tts voice")
            ttsStr = tostring(toneDowns)
            audio.play(TTS, "TTS", ttsStr, 7)
        end
        --if math.fmod(countAlarmDrill, 12) == 0 then uartTask.write(0x0C) end
        if toneBreak then
            --uartTask.write(0x0C)
            toneBreak = false
            toneDowns = 0
            break
        end
        sys.wait(1000)
        if toneBreak then
            --uartTask.write(0x0C)
            toneBreak = false
            toneDowns = 0
            break
        end
        toneDowns = toneDowns - 1
        --countAlarmDrill = countAlarmDrill + 1
    end
    setGpio9Fnc(1) -- 报警结束设置静音
    setGpio17Fnc(0) --音量测试恢复正常（大音量）
    setGpio13Fnc(0)
    doToneTask = true
end

-- 报警灯操作任务
local function alarmLampOperationTask()
    --local count = 0
    doLampTask = false
    lampDowns = 0
    log.info("alarmLamp enter")
    while lampDowns >= 0 and lampDowns <= 5400 do
        setGpio13Fnc(1)
        setGpio11Fnc(1)
        if lampBreak then
            lampBreak = false
            lampDowns = 0
            break
        end
        sys.wait(1000)
        if lampBreak then
            lampBreak = false
            lampDowns = 0
            break
        end
        lampDowns = lampDowns + 1
    end
    setGpio13Fnc(0)
    setGpio11Fnc(0)
    --[[ while lampDowns > 0 do
        while count ~= 4 do
            if lampBreak then
                break
            end
            setGpio12Fnc(1)
            sys.wait(125)
            setGpio12Fnc(0)
            sys.wait(125)
            count = count + 1
        end
        count = 0
        lampDowns = lampDowns - 1
        if lampBreak then
            lampBreak = false
            lampDowns = 0
            break
        end
        sys.wait(1000)
        if lampBreak then
            lampBreak = false
            lampDowns = 0
            break
        end
        lampDowns = lampDowns - 1
    end
    setGpio12Fnc(0)]]
    doLampTask = true
end

-- 获取震级颜色
local function getWarningColor(intensity)
    local warningColorGet = 0
    if intensity >= 1 and intensity <= 3 then
        warningColorGet = 1
    elseif intensity >= 4 and intensity <= 5 then
        warningColorGet = 2
    elseif intensity >= 6 and intensity <= 7 then
        warningColorGet = 3
    elseif intensity >= 8 then
        warningColorGet = 4
    end
    return warningColorGet
end

-- string转time
local function string2time(str)
    log.info("str", str)
    local dateStr, timeStr = "", ""
    dateStr = string.split(str, " ")[1]
    timeStr = string.split(str, " ")[2]
    local Y = string.split(dateStr, "-")[1]
    local m = string.split(dateStr, "-")[2]
    local d = string.split(dateStr, "-")[3]
    local H = string.split(timeStr, ":")[1]
    local M = string.split(timeStr, ":")[2]
    local S = string.split(timeStr, ":")[3]
    log.info("Y:", Y, "m:", m, "d:", d, "H:", H, "M:", M, "S:", S)
    return os.time({ day = d, month = m, year = Y, hour = H, min = M, sec = S })
end

--烈度回传事件
local function intensityReturnTask(intensity, tjsondata, earthquakeTopic, countDownS, quakeTimeStr,
                                   focal_longitude, distance, placeName, focal_latitude)
    sysTime = getSystemTime()
    local topic = "answer"
    local sendMsg = "{" .. "\"msgLogId\":" .. tostring(intensity) ..
        ",\"clientNo\":" .. tostring(warningColor) .. ",\"info\":\"{\"1\":\"" ..
        tostring(tjsondata["3"]) .. "\",\"2\":\"" .. tostring(tjsondata["2"]) .. "\",\"3\":\"" .. sysTime ..
        ",\"4\":" .. tostring(tjsondata["3"]) .. ",\"5\":" .. string.split(earthquakeTopic, "/")[2]
        .. "\",\"6\":\"2d89cc\",\"7\":\"1\",\"8\":\"1\",\"9\":\"" .. clientNo ..
        "\",\"10\":\"" .. tostring(tjsondata["16"]) .. "\",\"11\":\"1\"}\",\"result\":\"{'1':" ..
        countDownS .. ",'2':" .. intensity .. ",'3':" .. warningColor .. ",'4':" .. quakeTimeStr
        .. ",'5':" .. placeName .. ",'6':" .. tostring(magnitude) .. ",'7':" .. distance .. ",'8':"
        .. focal_longitude .. ",'9':" .. focal_latitude .. ",'10':" .. longitude .. ",'11':" ..
        latitude .. "}\",\"answerTime\":" .. os.time() * 1000 .. "}"
    sendMsg = crypto.aes_encrypt("ECB", "PKCS5", sendMsg, "keson-123abcdefg")
    sendMsg = crypto.base64_encode(sendMsg, slen(sendMsg))
    mqttOutMsg.insertMsg(topic, sendMsg, 0)
end

local function round(x) return x >= 0 and math.floor(x + 0.5) or math.ceil(x - 0.5) end

local function Algorithm(equipment_longitude, equipment_latitude, focal_longitude, focal_latitude)
    local Lat1 = math.rad(equipment_latitude)
    log.info("focal_latitude:", focal_latitude)
    local Lat2 = math.rad(focal_latitude)
    local a = Lat1 - Lat2
    local b = math.rad(equipment_longitude) - math.rad(focal_longitude)
    local s = 2 *
        math.asin(
            math.sqrt(math.pow(math.sin(a / 2), 2) + math.cos(Lat1) *
                math.cos(Lat2) *
                math.pow(math.sin(b / 2), 2)))
    s = s * 6378137.0
    s = round(s * 10000) / 10000; -- 精确距离的数值
    return s
end

--烈度转换为阈值
local function intensity2Threshold(intensity2Th)
    local threhold = 0
    if intensity2Th == 1 or intensity2Th == 2 then
        threhold = 1
    elseif intensity2Th == 3 or intensity2Th == 4 then
        threhold = 2
    elseif intensity2Th == 5 or intensity2Th == 6 then
        threhold = 3
    elseif intensity2Th >= 7 then
        threhold = 4
    end
    return threhold
end

--预警参数组装
local function alert_parameter_assembly(tjsondata)
    if regCenter then --启用注册中心
        if tjsondata["2"] then --消息类型赋值
            msgCode = tjsondata["2"]
        end
        log.info("msgCode", msgCode)
        if "0199" == msgCode then --预警取消
            eventCancelID    = tjsondata["7"]
            producerCancelID = tjsondata["8"]
        elseif "0101" == msgCode or "0201" == msgCode or "0301" == msgCode then --普通预警，普通预警演练，普通预警测试
            eventID = tjsondata["7"]
            producerID = tjsondata["9"]
            if tjsondata["14"] and tjsondata["15"] then
                focal_longitude = tonumber(tjsondata["14"])
                focal_latitude = tonumber(tjsondata["15"])
            end
            if tjsondata["18"] then
                quake_intensity = tonumber(tjsondata["18"])
            end
            if tjsondata["3"] then
                sendTime = tjsondata["3"]
            end
            if tjsondata["12"] then
                quakeTimeStr = tjsondata["12"]
            end
            if tjsondata["13"] then
                placeName = tjsondata["13"]
            end
            log.info("tjsondata", tjsondata["17"])
            if tjsondata["17"] then
                magnitude = tonumber(tjsondata["17"])
            end
            if tjsondata["16"] then
                depth = tjsondata["16"]
            end
        elseif "0102" == msgCode or "0202" == msgCode or "0302" == msgCode then --专业预警，专业预警演练，专业预警测试
            eventID = tjsondata["7"]
            producerID = tjsondata["10"]
            if tjsondata["12"] then
                sendTime = tjsondata["12"]
            end
            if tjsondata["16"] then
                placeName = tjsondata["16"]
            end
            if tjsondata["17"] and tjsondata["18"] then
                focal_longitude = tonumber(tjsondata["17"])
                focal_latitude = tonumber(tjsondata["18"])
            end
            if tjsondata["21"] then
                quake_intensity = tonumber(tjsondata["21"])
            end
            if tjsondata["15"] then
                quakeTimeStr = tjsondata["15"]
            end
            if tjsondata["19"] then
                depth = tjsondata["19"]
            end
            if tjsondata["20"] then
                magnitude = tonumber(tjsondata["20"])
            end
        end
    else --keson地震预警监控平台
        if tjsondata["7"] then
            focal_longitude = tonumber(tjsondata["7"])
        end
        if tjsondata["8"] then
            focal_latitude = tonumber(tjsondata["8"])
        end
        if tjsondata["11"] then
            quake_intensity = tonumber(tjsondata["11"])
        end
        if tjsondata["5"] then
            quakeTimeStr = tjsondata["5"]
            eventID = warningEventIdAssemble(quakeTimeStr)
        end
        if tjsondata["10"] then
            magnitude = tonumber(tjsondata["10"])
        end
        if tjsondata["13"] then
            placeName = tjsondata["13"]
        end
        if tjsondata["16"] then
            msgCode = tjsondata["16"]
        end
        sendTime = sysTime
        depth = "10"
    end
end

--预警取消
local function cancel_alert_event()
    if toneDowns > 0 then toneBreak = true end
    if lampDowns > 0 and lampDowns < 5400 then lampBreak = true end
    if signalDowns > 0 then signalBreak = true end
    while not doSignalTask do
        sys.wait(800)
    end
    if not send433RFSignal.TimeId then
        send433RFSignal.InterruptSendMsg = false
        send433RFSignal.sendMsgTask()
    end
end

--地震预警动作执行
local function earthquake_alert_action(aesDecryptMsg, tjsondata, earthquakeTopic)
    eqlog = eqlog .. "[" .. sysTime .. "]" .. "接收到的地震预警消息内容:" .. "\n" .. aesDecryptMsg
    if "false" == enable433 then
        enable433 = false
    end
    if "true" == enable433 then
        enable433 = true
    end
    actionMark = true
    if toneDowns > 0 then toneBreak = true end
    if lampDowns > 0 and lampDowns < 5400 then lampBreak = true end
    if signalDowns > 0 then signalBreak = true end
    local sysTimeOs = os.time()
    log.info("sysTime", sysTimeOs, "quakeTimeStr", quakeTimeStr)
    local quakeTime = string2time(quakeTimeStr)
    log.info("quakeTime", quakeTime)
    -- 计算距离
    local distance = Algorithm(longitude, latitude, focal_longitude, focal_latitude) / 1000
    log.info("distance", distance)
    -- 计算S波到达时间 减1秒网络延时
    local countDownS = math.floor((distance / 3.5) - (sysTimeOs - quakeTime))
    countDownS = countDownS < 0 and 0 or countDownS
    log.info("countDownS", countDownS)
    -- 烈度计算
    local intensityLocal = math.floor(round(
        quake_intensity - 4 *
        math.log10(distance / 10 + 1.0)))
    log.info("intensity", intensityLocal)
    if isEarthquakeReport then
        eathquakeWarningReportHttp(distance,"EEW",countDownS,distance)
    end
    if intensityLocal <= 0 then intensityLocal = 1 end
    if intensityLocal >= 1 and intensityLocal <= 4 then
        alertStr = "有感地震，注意防范"
    elseif intensityLocal > 4 then
        alertStr = "强破坏地震，紧急避险"
    end
    warningColor = getWarningColor(intensityLocal)
    eqlog = eqlog .. "[" .. sysTime .. "]" .. "[地震预警信息:]" .. "震源距离:" ..
        distance .. "KM,s波到达时间:" ..
        countDownS .. "s,烈度:" .. intensityLocal .. "\n"
    -- 地震烈度大于等于设定预警临界值则执行报警
    if intensityLocal >= configIntensity and magnitude >= magnitudeConfig and
        quake_intensity >= centerIntensityConfig then
        eqlog = eqlog .. "[" .. sysTime .. "]" .. "本地计算-达到本地预警阀值！\n"
        if countDownS > 0 then
            while not doLampTask do
                sys.wait(800)
            end
            if intensity2Threshold(intensityLocal) >= alertSound and doToneTask then
                if "0101" == msgCode or "0102" == msgCode or not regCenter then --普通预警，专业预警,keson转发预警
                    eqlog = eqlog .. "[" .. sysTime .. "]" .. "本地计算-达到本地预警音阀值！\n"
                    sys.wait(300)
                    sys.taskInit(alarmVoiceTask, countDownS)
                    log.info("alarmVoiceTaskEND", alarmVoiceTask)
                elseif "0201" == msgCode or "0202" == msgCode then --普通预警演习，专业预警演习
                    ttsStr = "预警系统演练。预警系统演练。预警系统演练"
                    sys.wait(300)
                    sys.taskInit(alarmDrillVoiceTask, countDownS)
                elseif "0301" == msgCode or "0302" == msgCode then --普通预警测试，专业预警测试
                    ttsStr = "预警系统测试。预警系统测试。预警系统测试"
                    sys.wait(300)
                    sys.taskInit(alarmDrillVoiceTask, countDownS)
                end
            else
                eqlog = eqlog .. "[" .. sysTime .. "]" .. "[不满足报警音临界值：]" .. "烈度阈值："
                    .. intensity2Threshold(intensityLocal) .. ",报警音预设值：" .. alertSound ..
                    "[执行报警音任务:]" .. tostring(doToneTask) .. "\n"
            end
            --报警灯任务
            log.info("alertLight",alertLight,"intensityLocal",intensityLocal)
            if intensityLocal >= alertLight and doLampTask then
                eqlog = eqlog .. "[" .. sysTime .. "]" .. "本地计算-达到本地预警灯阀值！\n"
                sys.wait(200)
                sys.taskInit(alarmLampOperationTask)
            else
                eqlog = eqlog .. "[" .. sysTime .. "]" .. "[不满足报警灯临界值：]" ..
                    "烈度：" .. intensityLocal ..
                    ",报警灯预设值：" .. alertLight .. "[执行报警灯任务:]" ..
                    tostring(doLampTask) .. "\n"
            end
            while not doSignalTask do
                sys.wait(800)
            end
            --433信号发送任务
            if intensity2Threshold(intensityLocal) >= emergencyLamp and enable433 and doSignalTask then
                eqlog = eqlog .. "[" .. sysTime .. "]" .. "本地计算-达到本地发送433所需条件阀值！\n"
                sys.wait(200)
                sys.taskInit(send433SignalTask, countDownS, intensityLocal)
            else
                eqlog = eqlog .. "[" .. sysTime .. "]" .. "[阈值，发送433，执行发送433信号存在条件不满足]"
                    .. "烈度转阈值：" .. intensity2Threshold(intensityLocal) .. ",设定阈值：" ..
                    emergencyLamp .. "是否发送433:" ..
                    enable433 .. "执行发送433信号任务" .. tostring(doSignalTask) .. "\n"
            end
            while not doToneTask do
                sys.wait(800)
            end
            --sys.taskInit(mqttQuakAlarmSendTask, intensity, countDownS)
        end
    else
        eqlog = eqlog .. "[" .. sysTime .. "]" .. "[不满足预警临界值：]" .. "烈度：" .. intensityLocal ..
            ",烈度预设值：" .. configIntensity .. ",震级:" .. magnitude .. ",震级预设值:" ..
            magnitudeConfig ..
            ",震中烈度:" .. quake_intensity .. ",震中烈度预设值:" .. centerIntensityConfig .. "\n"
    end
    --烈度回传事件
    if intensityLocal >= intensityReturn then

        log.info("intensityReturn", intensityReturn)
        intensityReturnTask(intensityLocal, tjsondata, earthquakeTopic, countDownS, quakeTimeStr,
            focal_longitude, distance, placeName, focal_latitude)
        -- sys.taskInit(intensityReturnTask, intensity, tjsondata, earthquakeTopic, countDownS, quakeTimeStr,
        --     focal_longitude, distance, placeName, focal_latitude)
    else
        eqlog = eqlog .. "[" .. sysTime .. "]" .. "[不满足烈度回传临界值：]" .. "烈度：" ..
            intensityLocal ..
            ",烈度回传预设值：" .. intensityReturn .. "\n"
    end
    actionMark = false
end

-- 地震预警主逻辑
function EarthquakeWarning(aesDecryptMsg, tjsondata, earthquakeTopic)
    errlog, eqlog = "", ""
    if "true" == regCenter then
        regCenter = true
    elseif "false" == regCenter then
        regCenter = false
    end
    sysTime = getSystemTime()
    alert_parameter_assembly(tjsondata)
    log.info("Equipment_latitude", getLbsLoc.Lat, "Equipment_longitude", getLbsLoc.Lng)
    if longitude == "" and latitude == "" then
        longitude = getLbsLoc.Lng
        latitude = getLbsLoc.Lat
    end
    eqlog = eqlog .. "[" .. sysTime .. "]" .. "[设备位置信息:]" ..
        "Lng:" .. longitude .. ",Lat:" .. latitude .. "\n"
    if regCenter then --启用注册中心
        log.info("regCenter", regCenter)
        log.info("eventCancelID", eventCancelID, "eventID", eventID, "producerID", producerID, "producerCancelID",
            producerCancelID)
        if "0199" == msgCode then
            if eventCancelID == eventID and producerID == producerCancelID then
                cancel_alert_event() --取消地震预警
            end
        else
            while actionMark do
                sys.wait(800)
            end
            sys.taskInit(earthquake_alert_action, aesDecryptMsg, tjsondata, earthquakeTopic) --地震预警动作执行
            log.info("地震预警动作执行结束")
        end
    else --keson转发地震预警信息
        if "0199" == msgCode or quake_intensity == 0 then
            cancel_alert_event()
        else
            while actionMark do
                sys.wait(800)
            end
            sys.taskInit(earthquake_alert_action, aesDecryptMsg, tjsondata, earthquakeTopic)
        end
    end
    if eqlog ~= nil and eqlog ~= false then
        logGenerator.generateLogTask(eqlog, "log")
    end
    if errlog ~= "" and errlog ~= false then
        logGenerator.generateLogTask(errlog, "log")
    end
end
